// GENERAL
$('body').append('<div id="alert-box" class="hidden"><div class="message"></div></div><span id="loadingtext"></span>');

// Toolpik
$('abbr, acronym').css('cursor', 'help').hover(function(e) {
	var winWidth = $(window).width(),
		winHeight = $(window).height(),
		ttImg = 'http://2.bp.blogspot.com/-adzEOrQwrZI/TulXSIRL_II/AAAAAAAABn0/8HMRpiW6F-s/s1600/ikon1.png';
	$(this).append('<div id="toolpik"><div id="nurrohman"></div><div style="clear:both;"></div><img class="netral absol" src="' + ttImg + '" alt="" /></div>');
	$('#nurrohman', '#toolpik').html($(this).attr('title'));
	$(this).removeAttr('title');
	var toolpikWidth = $("#toolpik").outerWidth(),
		toolpikHeight = $("#toolpik").outerHeight(),
		top = e.clientY - toolpikHeight / 2,
		left = e.clientX - toolpikWidth / 2;
	if (top + toolpikHeight > winHeight) {
		top = winHeight - toolpikHeight - 50;
	} else if (top <= 0) {
		top = 10;
	}
	if (left + toolpikWidth > winWidth) {
		left = winWidth - toolpikWidth - 50;
	} else if (left <= 0) {
		left = 10;
	}
	function movement() {
		$(this).delay(600).animate({
			top: top - 70,
			left: left
		}, {
			duration: 2000,
			easing: "easeOutBack"
		}).animate({
			top: top
		}, 400);
	}
	$('#toolpik').delay(600).fadeIn(1000, movement);
}, function() {
	$(this).attr('title', $('#nurrohman', '#toolpik').html());
	$('#nurrohman').parent().remove();
});

// Spoiler
$('.spoiler').hide().before('<a class="sp-trigger" href="#" style="font-weight:bold;">Lihat Konten [+]</a>');
$('.sp-trigger').toggle(function() {
	$(this).text('Tutup [-]').next().show();
	return false;
}, function() {
	$(this).text('Lihat Konten [+]').next().hide();
	return false;
});

// Post & Comments Manipulation
setTimeout(function() {

	function r(selector, tag) {
			$('#comments p').each(function() {
				$('b[rel^="h"]', this).next('br').remove();
				$(selector, this).replaceWith(function() {
					return $(tag).append($(this).contents());
				});
				$(this).html(
					$(this).html()
						.replace(/\[iframe\](.[^\]]*)\[\/iframe\]/ig, "<iframe class='loader video' lsrc='$1'><\/iframe>")
						.replace(/\[note\]/ig, "<div class='note'>")
						.replace(/\[\/note\]/ig, "</div>")
				);
			});
		}

	function e(emo, imgRep, emoKey) {
			$('#comments p').each(function() {
				$(this).html(
					$(this).html()
						.replace(/@<a.*>.*[^>]<\/a>/g, "")
						.replace(/<br>+(\:|\;|\=|\^)/g, "<br> $1")
						.replace(/^(&nbsp;|<br>| <br>)+/g, '')
						.replace(emo, " <img src='http://hompimpa.googlecode.com/svn/trunk/personal/emo/" + imgRep + "' class='emo delayLoad' alt='" + emoKey + "' />")
				);
			});
		}

	e(/\s:\)+/g, "smile.gif", ":)");
	e(/\s;\)+/g, "wink.gif", ";)");
	e(/\s:\(/g, "sad.gif", ":(");
	e(/\s=\(/g, "sadanimated.gif", "=(");
	e(/\s@@,/g, "rolleyes.gif", "@@,");
	e(/\s:yaya:/ig, "yaya.gif", ":yaya:");
	e(/\s:s/ig, "sullen.gif", ":s");
	e(/\s:(\\|\/)/g, "memble.gif", ":&#92;");
	e(/\s:D/g, "haha.gif", ":D");
	e(/\s=D/g, "hihi.gif", "=D");
	e(/\s\^:D/g, "abovemehaha.gif", "^:D");
	e(/\s\^(\_|)\^/g, "senyum-tulus.gif", "^_^");
	e(/\s:'\(/g, "cry.gif", ":&#39;(");
	e(/\s:waaa:/g, "wawa.gif", ":waaa:");
	e(/\sT_T/ig, "tears.gif", "T_T");
	e(/\sB\)/g, "cool.gif", "B)");
	e(/\s:Q/ig, "smoking.gif", ":Q");
	e(/\s\*\*p/ig, "crazy.gif", "**p");
	e(/\s7:\(/g, "conf.gif", "7:(");
	e(/\s:p/ig, "wee.gif", ":p");
	e(/\s:Oz+/ig, "sleep.gif", ":Ozz");
	e(/\s7:O/ig, "angry.gif", "7:O");
	e(/\s\\o\//ig, "applause.gif", "&#92;o/");
	e(/\s\\m\//ig, "metal.gif", "&#92;m/");
	e(/\s(&lt;3|:\*)/ig, "love.gif", "&amp;amp;lt;3");
	e(/\s0:\)+/ig, "angelgreen.gif", "0:)");
	e(/\s\^o\^/ig, "angelwhite.gif", "^o^");
	e(/\s:-a/ig, "vomit.gif", ":-a");
	e(/\s\*fck\*/ig, "fuck.gif", "*fck*");
	e(/\sxV/ig, "demo.gif", "xV");
	e(/\sx\@/g, "marahbesar.gif", "x@");
	e(/\s\X\@/g, "arrgh.gif", "X@");
	e(/\s:-d/ig, "top.gif", ":-d");
	e(/\s:-bd/ig, "topmarkotop.gif", ":-bd");
	e(/\s\~x\(+/ig, "ugh.gif", "~x(");
	e(/\s:bye:/ig, "bye.gif", ":bye:");
	e(/\s:W/g, "explain.gif", ":W");
	e(/\s\(-\.-,\)/g, "pumpkin.gif", "(-.-,)");
	e(/\s\*=p/ig, "ball1.gif", "*=p");
	e(/\s=p\*/ig, "ball2.gif", "=p*");
	e(/\s:(imhere|disini):/ig, "imhere.gif", ":disini:");
	e(/\s:cendol:/ig, "cendol.gif", ":cendol:");
	e(/\s&lt;:\)/ig, "festival1.gif", "&amp;amp;lt;:)");
	e(/\s&lt;=\)/ig, "festival2.gif", "&amp;amp;lt;=)");
	e(/\s:(rolled|guling):/ig, "rolled.gif", ":guling:");
	e(/\s\*bang\*/ig, "bang.gif", "*bang*");
	e(/\s\'\'J/ig, "call.gif", "&#39;&#39;J");
	e(/\s:drummer:/ig, "drummer.gif", ":drummer:");
	e(/\s:(guitarist|gitaris):/ig, "guitarist.gif", ":gitaris:");
	e(/\s:(vocalist|vokalis):/ig, "vocalist.gif", ":vokalis:");
	// SPAMMER WEEEKKK!!!
	$('.cm_entry .komentar-spam').each(function() {
		var content = $(this).html(),
			allowlinks = $('a:not("a.allow")', this);
		if (allowlinks.length > 0) {
			$(this).parents('.cm_wrap').find('.cm_reply a').remove();
			$(this).addClass('deleted-by-js').html('Komentar Anda telah dihapus/disembunyikan untuk alasan keamanan dan kenyamanan pembaca lain. Kemungkinan hal ini terjadi karena Anda menuliskan komentar disertai dengan munculnya tautan-tautan yang tidak diperlukan pembaca/tidak sesuai dengan diskusi. Tapi jika bukan itu masalahnya, Saya harap Anda bersedia mencoba menuliskan komentar Anda kembali tanpa mencantumkan tautan/link aktif yang tidak diperlukan. Berikut ini adalah salinan yang mungkin Anda perlukan dari komentar lama yang baru Anda tuliskan sebelum ini:<textarea spellcheck="false">' + content.replace(/<br>/ig, "\n") + '</textarea>Terima kasih banyak atas kontribusi Anda di blog eksperimen ini. Salam, <strong>Administrator <big>&hearts;</big></strong><br>');
		}
	});

	r("i[rel='code']", '<code></code>');
	r("i[rel='pre']", '<pre></pre>');
	r("b[rel='h1']", '<h1></h1>');
	r("b[rel='h2']", '<h2></h2>');
	r("b[rel='h3']", '<h3></h3>');
	r("b[rel='h4']", '<h4></h4>');
	r("b[rel='h5']", '<h5></h5>');
	r("b[rel='h6']", '<h6></h6>');
	r("b[rel='quote']", '<blockquote></blockquote>');
	r("em[rel='note']", '<div class="note"></div>');
	$('i[rel="image"]').replaceWith(function() {
		return $('<a class="allow center" href="' + $(this).text() + '" target="_blank"><img class="incomment center" alt="screenshot" src="' + $(this).text().replace(/\/s1600/g, "\/s400").replace(/\/$/, "") + '" /></a>');
	});
	$('i[rel="anchor"]').replaceWith(function() {
		return $('<a class="allow" href="' + $(this).text() + '" rel="nofollow">[Link]</a>');
	});
	$('.post-body, .post-body div, .post-body li, .cm_entry p').children('code').each(function() {
		$(this).attr('title', 'Telusuri makna kode ini').wrapInner('<a class="allow" href="/p/cari-artikel.html?q=' + encodeURIComponent($(this).text()) + '"></a>');
	});

	$('.emo').on("click", function() {
		$('.emoKey').remove();
		$(this).after('<input class="emoKey" type="text" size="6" value=" ' + this.alt + '" />');
	});
	$('#comments').on("click", ".emoKey", function() {
		$(this).focus().select();
	});
	$('#respond p b').one("click", function() {
		$('#alert-box').fadeIn(600).find('.message').html('Untuk menyisipkan emotikon setidaknya Anda harus menambahkan satu spasi di depan. Jangan pernah melupakan itu atau emotikon Anda tidak bisa diterjemahkan<div class="button-group"><button class="close">OK</button></div>');
	});
	$('#alert-box').on("click", ".close", function() {
		$(this).parents('#alert-box').fadeOut();
	});

	$('.jsfiddle-demo').each(function() {
		$(this).replaceWith('<iframe class="jsfiddle-demo loader" src="' + $(this).data('src') + '" allowfullscreen="allowfullscreen"></iframe>');
	});

}, 5000); // End set timeout.


// Preloading...
$(window).on("load", function() {
	$('iframe.video').each(function() {
		$(this).attr('src', $(this).attr('lsrc'));
	});
	setTimeout(function() {
		$('iframe.video:not([src*="youtube.com/embed"])').removeAttr('src').css('background', '#900 url(http://hompimpa.googlecode.com/svn/trunk/personal/images/iframe-fallback.png) no-repeat 50% 50%');
	}, 2000);
});
